package com.mysql.opt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


import object.model.MySQL_TableDDL;
import object.model.TableType;

import reflect.engine.DataBaseStructInfoer;
import system.Sys_Config;
import system.mysql.schema.information.models.COLUMNS;
import system.mysql.schema.information.models.STATISTICS;
import system.mysql.schema.information.models.TABLES;


public class TablesOperator	extends Operator {
	
	private List<TABLES> tbList = null;
	
	public static void main(String[] args){
		
	}
	
	
	public TablesOperator(){
		this.oprType = "Table";
		this.refresh();
	}
	
	@Override
	public boolean drop(int index) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean dropAll() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<String[]> getBackupSQLs() {
		// TODO Auto-generated method stub
		List<String[]> SQLs4b = new ArrayList<String[]>();
		for(int i=0;i<this.tbList.size();i++){
			TABLES tb = this.tbList.get(i);
			String createStatement = "";
			createStatement += this.genComment(tb) + Operator.nextline;
			createStatement += this.genCreateStatement(tb);
			String[] back = {"TABLE: "+tb.getTABLE_NAME(),createStatement};
			SQLs4b.add(back);
		}
		return SQLs4b;
	}

	@Override
	public List<String> getNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getObjs() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		DataBaseStructInfoer dbti = new DataBaseStructInfoer();
		this.tbList = dbti.getBaseTables();
	}
	
	private String genCreateStatement(TABLES tb){
		String stat = "";		
		MySQL_TableDDL ddl = new DataBaseStructInfoer().getTableDDL(tb, TableType.valueOfDesc(tb.getTABLE_TYPE()));
		stat += ddl.getDropStatement()+ ";" + Operator.nextline;;
		stat += ddl.getCreateTable() + ";" + Operator.nextline;;
		return stat;
	}
	
	private String genPreStatement(){
		return "SET FOREIGN_KEY_CHECKS=0;";
	}
	
	private String genComment(TABLES tb){
		return "/*TABLE : `" + tb.getTABLE_SCHEMA() + "`.`" + tb.getTABLE_NAME() + "`*/";
	}
	
}
